package com.chinaoly.sdk.base;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by chenjian on 2019/6/3 13:36.
 */
public abstract class DisposeViewModel extends AndroidViewModel {
    private CompositeDisposable compositeDisposable = new CompositeDisposable();


    public DisposeViewModel(@NonNull Application application) {
        super(application);
    }

    @CallSuper
    @Override
    protected void onCleared() {
        super.onCleared();
        if (compositeDisposable != null) {
            compositeDisposable.clear();
            compositeDisposable = null;
        }
    }
    protected void addDispose(Disposable disposable){
        if (compositeDisposable != null&&!compositeDisposable.isDisposed()) {
            compositeDisposable.add(disposable);
        }
    }
}
